package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.ExcelUtil;
import com.ybkj.daijia.ServiceSetting;
import com.ybkj.daijia.serverUtils.ServiceUtil;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.chart.DriverTimeVo;
import com.ybkj.daijia.chart.HourChart;
import com.ybkj.daijia.chart.TableOrderMonthVo;
import com.ybkj.daijia.common.CompanyOrderVo;
import com.ybkj.daijia.common.DirverStatistics;
import com.ybkj.daijia.common.DriverOrderDetailVo;
import com.ybkj.daijia.common.DriverOrderVo;
import com.ybkj.daijia.common.InfoFeeVo;
import com.ybkj.daijia.common.MemberOrderVo;
import com.ybkj.daijia.common.OrderFromSourceVo;
import com.ybkj.daijia.common.PageVo;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.PointVo;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.mapper.DriverMapper;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.ChartDay;
import com.ybkj.daijia.server.model.ChartMonth;
import com.ybkj.daijia.server.model.ChartYear;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.OrderFinishedInfo;
import com.ybkj.daijia.server.sales.Activity;
import com.ybkj.daijia.server.sales.Activity.ActivityType;
import com.ybkj.daijia.server.service.ChartYearService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.DriverTimeService;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderFinishedInfoService;
import com.ybkj.daijia.server.service.OrderService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "chart")
public class ChartController {

    @Autowired
    private ChartYearService chartYearService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private ZhuancheService zhuancheService;
    @Autowired
    private ErrandService errandService;
    @Autowired
    private FreightService freightService;
    @Autowired
    private ZhuanxianService zhuanxianService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private DriverTimeService driverTimeService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private OrderFinishedInfoService orderFinishedInfoService;


    /**
     * 订单年统计图
     *
     * @return
     */
    @RequestMapping("orderYearChart")
    public String orderYearChart(Model model, Integer year, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            List<Integer> years = new LinkedList<Integer>();

            for (int i = calendar.get(Calendar.YEAR); i >= 2014; i--) {
                years.add(i);
            }
            model.addAttribute("years", years);
            if (year == null) {
                model.addAttribute("searchYear", calendar.get(Calendar.YEAR));
            } else {
                model.addAttribute("searchYear", year);
            }
            model.addAttribute("companys", companyService.findAll());
            companyId = principal.getCompanyId();
            model.addAttribute("companyId", companyId);

            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            if (serviceSetting.isDaijia()) {
                model.addAttribute("activityTab", "daijia");
            } else if (serviceSetting.isZhuanche()) {
                model.addAttribute("activityTab", "zhuan");
            } else if (serviceSetting.isPaotui()) {
                model.addAttribute("activityTab", "errand");
            } else if (serviceSetting.isFreight()) {
                model.addAttribute("activityTab", "freight");
            } else if (serviceSetting.isZhuanxian()) {
                model.addAttribute("activityTab", "zhuanxian");
            }

            return "chart/yearOrder";
        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping("dataForYear")
    public @ResponseBody
    ChartYear chartDataForYear(@RequestParam(required = false) Integer year,
        @RequestParam(required = false) Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        if (principal.isCompanyType()) {

            ChartYear chartYear = chartYearService.dataForYear(year, companyId);

            return chartYear;

        } else {
            ChartYear chartYear = chartYearService.dataForYear(year, principal.getCompanyId());

            return chartYear;
        }

    }

    @RequestMapping(value = "errandDataForYear", method = RequestMethod.POST)
    public @ResponseBody
    ChartYear errandChartDataForYear(@RequestParam(required = false) Integer year,
        @RequestParam(required = false) Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }

        if (principal.isCompanyType()) {

            ChartYear chartYear = errandService.dataForYear(year, companyId);

            return chartYear;

        } else {
            ChartYear chartYear = errandService.dataForYear(year, principal.getCompanyId());

            return chartYear;
        }

    }

    @RequestMapping(value = "zhuancheDataForYear", method = RequestMethod.POST)
    public @ResponseBody
    ChartYear zhuancheDataForYeara(@RequestParam(required = false) Integer year,
        @RequestParam(required = false) Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }

        if (principal.isCompanyType()) {

            ChartYear chartYear = zhuancheService.dataForYear(year, companyId);

            return chartYear;

        } else {
            ChartYear chartYear = zhuancheService.dataForYear(year, principal.getCompanyId());

            return chartYear;
        }

    }

    @RequestMapping(value = "zhuanxianDataForYear", method = RequestMethod.POST)
    public @ResponseBody
    ChartYear zhuanxianDataForYeara(@RequestParam(required = false) Integer year,
        @RequestParam(required = false) Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }

        if (principal.isCompanyType()) {

            ChartYear chartYear = zhuanxianService.dataForYear(year, companyId);

            return chartYear;

        } else {
            ChartYear chartYear = zhuanxianService.dataForYear(year, principal.getCompanyId());

            return chartYear;
        }

    }

    @RequestMapping(value = "freightDataForYear", method = RequestMethod.POST)
    public @ResponseBody
    ChartYear freightDataForYeara(@RequestParam(required = false) Integer year,
        @RequestParam(required = false) Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }

        if (principal.isCompanyType()) {

            ChartYear chartYear = freightService.dataForYear(year, companyId);

            return chartYear;

        } else {
            ChartYear chartYear = freightService.dataForYear(year, principal.getCompanyId());

            return chartYear;
        }

    }


    @RequestMapping(value = "errandDataForMonth", method = RequestMethod.POST)
    public @ResponseBody
    ChartMonth errandDataForMonth(
        @RequestParam("year") Integer year,
        @RequestParam(required = false) Long companyId,
        @RequestParam("month") Integer month) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartMonth chartMonth = null;
        if (principal.isCompanyType()) {

            chartMonth = errandService.errandDataForMonth(year, month, companyId);

            return chartMonth;

        } else {
            chartMonth = errandService.errandDataForMonth(year, month, principal.getCompanyId());

            return chartMonth;
        }

    }

    @RequestMapping(value = "zhuancheDataForMonth", method = RequestMethod.POST)
    public @ResponseBody
    ChartMonth zhuancheDataForMonth(
        @RequestParam("year") Integer year,
        @RequestParam(required = false) Long companyId,
        @RequestParam("month") Integer month) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartMonth chartMonth = null;
        if (principal.isCompanyType()) {

            chartMonth = zhuancheService.zhuancheDataForMonth(year, month, companyId);

            return chartMonth;

        } else {
            chartMonth = zhuancheService
                .zhuancheDataForMonth(year, month, principal.getCompanyId());

            return chartMonth;
        }

    }

    @RequestMapping(value = "zhuanxianDataForMonth", method = RequestMethod.POST)
    public @ResponseBody
    ChartMonth zhuanxianDataForMonth(
        @RequestParam("year") Integer year,
        @RequestParam(required = false) Long companyId,
        @RequestParam("month") Integer month) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartMonth chartMonth = null;
        if (principal.isCompanyType()) {

            chartMonth = zhuanxianService.zhuanxianDataForMonth(year, month, companyId);

            return chartMonth;

        } else {
            chartMonth = zhuanxianService
                .zhuanxianDataForMonth(year, month, principal.getCompanyId());

            return chartMonth;
        }

    }

    @RequestMapping(value = "freightDataForMonth", method = RequestMethod.POST)
    public @ResponseBody
    ChartMonth freightDataForMonth(
        @RequestParam("year") Integer year,
        @RequestParam(required = false) Long companyId,
        @RequestParam("month") Integer month) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartMonth chartMonth = null;
        if (principal.isCompanyType()) {

            chartMonth = freightService.freightDataForMonth(year, month, companyId);

            return chartMonth;

        } else {
            chartMonth = freightService.freightDataForMonth(year, month, principal.getCompanyId());

            return chartMonth;
        }

    }

    @RequestMapping("dataForDay")
    public @ResponseBody
    ChartDay dataForDay(Integer year, Integer month, Integer day, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartDay chartDay = null;
        if (principal.isCompanyType()) {
            chartDay = chartYearService.dataForDay(year, month, day, companyId);
        } else {
            chartDay = chartYearService.dataForDay(year, month, day, principal.getCompanyId());
        }
        return chartDay;
    }

    @RequestMapping(value = "errandDataForDay", method = RequestMethod.POST)
    public @ResponseBody
    ChartDay errandDataForDay(Integer year, Integer month, Integer day, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }

        ChartDay chartDay = null;
        if (principal.isCompanyType()) {
            chartDay = errandService.errandDataForDay(year, month, day, companyId);

        } else {
            chartDay = errandService.errandDataForDay(year, month, day, principal.getCompanyId());
        }

        return chartDay;
    }

    @RequestMapping(value = "zhuancheDataForDay", method = RequestMethod.POST)
    public @ResponseBody
    ChartDay zhuancheDataForDay(Integer year, Integer month, Integer day, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartDay chartDay = null;
        if (principal.isCompanyType()) {
            chartDay = zhuancheService.zhuancheDataForDay(year, month, day, companyId);
        } else {
            chartDay = zhuancheService
                .zhuancheDataForDay(year, month, day, principal.getCompanyId());
        }
        return chartDay;
    }

    @RequestMapping(value = "freightDataForDay", method = RequestMethod.POST)
    public @ResponseBody
    ChartDay freightDataForDay(Integer year, Integer month, Integer day, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartDay chartDay = null;
        if (principal.isCompanyType()) {
            chartDay = freightService.freightDataForDay(year, month, day, companyId);
        } else {
            chartDay = freightService.freightDataForDay(year, month, day, principal.getCompanyId());
        }
        return chartDay;
    }

    @RequestMapping(value = "zhuanxianDataForDay", method = RequestMethod.POST)
    public @ResponseBody
    ChartDay zhuanxianDataForDay(Integer year, Integer month, Integer day, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (null == year) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        ChartDay chartDay = null;
        if (principal.isCompanyType()) {
            chartDay = zhuanxianService.zhuanxianDataForDay(year, month, day, companyId);
        } else {
            chartDay = zhuanxianService
                .zhuanxianDataForDay(year, month, day, principal.getCompanyId());
        }
        return chartDay;
    }

    //员工业绩初始化
    @RequestMapping("driverOrder")
    public String driverOrder(Model model) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            model.addAttribute("companys", companyService.findAll());
			/*if(principal.isCompanyType()){
				model.addAttribute("totalElements", chartYearService.driverOrder(null, null,null,null).getTotalElements());
			}else{
				model.addAttribute("totalElements", chartYearService.driverOrder(null, null,principal.getCompanyId(),null).getTotalElements());
			}
			*/
            return "chart/driverOrder";

        } else {
            throw new NoPermissionException();
        }

    }

    //员工业绩
    @RequestMapping("driverOrderData")
    public @ResponseBody
    PageVo<DirverStatistics> driverOrderData(
        @RequestParam(required = false, value = "time") String time,
        @RequestParam(required = false, value = "day") String day,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "driverName") String name,
        @RequestParam(required = false, value = "endTime") String endTime) {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Date start = null;
            Date end = null;
            if (StringUtils.isBlank(time)) {
                if (!StringUtils.isEmpty(startTime)) {

                    try {
                        start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }

                if (!StringUtils.isBlank(endTime)) {

                    try {
                        end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }

            }

            Setting setting = settingUtils.get();

            if ("today".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                start = calendar.getTime();
                end = DateUtils.addHours(start, 24);

            } else if ("yesterday".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                end = calendar.getTime();
                start = DateUtils.addHours(end, -24);

            } else if ("month".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.HOUR_OF_DAY, 24);

                end = calendar.getTime();

                calendar.add(Calendar.HOUR_OF_DAY, -24);
                calendar.set(Calendar.DAY_OF_MONTH, 1);

                start = calendar.getTime();

            } else if ("lastmonth".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                end = calendar.getTime();
                start = DateUtils.addMonths(end, -1);
            }
            PageVo<DirverStatistics> result = null;

            int offset = Pager.getOffset();
            int size = Pager.getLimit();

            if (principal.isCompanyType()) {
                result = chartYearService.driverOrder(start, end, companyId, name, offset, size);
            } else {
                result = chartYearService
                    .driverOrder(start, end, principal.getCompanyId(), name, offset, size);
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //员工业绩
    @RequestMapping("zhuanDriverOrderData")
    public @ResponseBody
    PageVo<DirverStatistics> zhuanDriverOrderData(
        @RequestParam(required = false, value = "time") String time,
        @RequestParam(required = false, value = "day") String day,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "driverName") String name,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Date start = null;
            Date end = null;
            if (StringUtils.isBlank(time)) {
                if (!StringUtils.isEmpty(startTime)) {

                    try {
                        start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(start);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);

                        start = calendar.getTime();

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                } else {
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.add(Calendar.DAY_OF_MONTH, -30);
                    start = calendar.getTime();

                }

                if (!StringUtils.isBlank(endTime)) {

                    try {
                        end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(end);
                        calendar.set(Calendar.HOUR_OF_DAY, 23);
                        calendar.set(Calendar.MINUTE, 59);
                        calendar.set(Calendar.SECOND, 59);

                        end = calendar.getTime();

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                } else {
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    end = calendar.getTime();

                }

            }

            Setting setting = settingUtils.get();

            if ("today".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                start = calendar.getTime();
                end = DateUtils.addHours(start, 24);

            } else if ("yesterday".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                end = calendar.getTime();
                start = DateUtils.addHours(end, -24);

            } else if ("month".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.HOUR_OF_DAY, 24);

                end = calendar.getTime();

                calendar.add(Calendar.HOUR_OF_DAY, -24);
                calendar.set(Calendar.DAY_OF_MONTH, 1);

                start = calendar.getTime();

            } else if ("lastmonth".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                end = calendar.getTime();
                start = DateUtils.addMonths(end, -1);
            }
            PageVo<DirverStatistics> result = null;

            int offset = Pager.getOffset();
            int size = Pager.getLimit();

            if (principal.isCompanyType()) {
                result = chartYearService.driverOrder(start, end, companyId, name, offset, size);
            } else {
                result = chartYearService
                    .driverOrder(start, end, principal.getCompanyId(), name, offset, size);
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //导出员工业绩数据
    @RequestMapping("exportDriverOrder")
    public void exportDriverOrder(
        @RequestParam(required = false, value = "time") String time,
        @RequestParam(required = false, value = "day") String day,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "driverName") String name,
        @RequestParam(required = false, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (StringUtils.isBlank(time)) {
                if (!StringUtils.isEmpty(startTime)) {

                    try {
                        start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }

                if (!StringUtils.isBlank(endTime)) {

                    try {
                        end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }

            }

            Setting setting = settingUtils.get();

            if ("today".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                start = calendar.getTime();
                end = DateUtils.addHours(start, 24);

            } else if ("yesterday".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                end = calendar.getTime();
                start = DateUtils.addHours(end, -24);

            } else if ("month".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.HOUR_OF_DAY, 24);

                end = calendar.getTime();

                calendar.add(Calendar.HOUR_OF_DAY, -24);
                calendar.set(Calendar.DAY_OF_MONTH, 1);

                start = calendar.getTime();

            } else if ("lastmonth".equals(time)) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                end = calendar.getTime();
                start = DateUtils.addMonths(end, -1);
            }
            PageVo<DirverStatistics> list = null;
            if (principal.isCompanyType()) {
                list = chartYearService.driverOrder(start, end, companyId, name, null, null);
            } else {
                list = chartYearService
                    .driverOrder(start, end, principal.getCompanyId(), name, null, null);
            }

            String[] head = new String[18];
            ArrayList<String> headcell = new ArrayList<String>();
            List<List<String>> rowlist = new ArrayList<List<String>>();
            headcell.add("服务人员(工号)");
            headcell.add("类型");
            headcell.add("所属公司");
            head[0] = (" ");
            head[1] = (" ");
            head[2] = (" ");
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            int serviceCount = 0;
            if (serviceSetting.isDaijia()) {
                serviceCount++;
            }
            if (serviceSetting.isPaotui()) {
                serviceCount++;
            }
            if (serviceSetting.isZhuanche()) {
                serviceCount++;
            }
            if (serviceSetting.isFreight()) {
                serviceCount++;
            }
            if (serviceSetting.isZhuanxian()) {
                serviceCount++;
            }
            boolean daijia = true, pao = true, zhuan = true, freight = true, zhuanxian = true;
            for (int i = 1; i <= serviceCount; i++) {
                int temp = i * 3;
                if (serviceSetting.isDaijia() && daijia) {
                    head[temp] = (" ");
                    head[temp + 1] = ("代驾");
                    head[temp + 2] = (" ");
                    headcell.add("完成单量");
                    headcell.add("订单金额");
                    headcell.add("提成");
                    daijia = false;
                    continue;
                }
                if (serviceSetting.isPaotui() && pao) {
                    head[temp] = (" ");
                    head[temp + 1] = ("跑腿");
                    head[temp + 2] = (" ");
                    headcell.add("完成单量");
                    headcell.add("订单金额");
                    headcell.add("提成");
                    pao = false;
                    continue;
                }
                if (serviceSetting.isZhuanche() && zhuan) {
                    head[temp] = (" ");
                    head[temp + 1] = ("专车");
                    head[temp + 2] = (" ");
                    headcell.add("完成单量");
                    headcell.add("订单金额");
                    headcell.add("提成");
                    zhuan = false;
                    continue;
                }
                if (serviceSetting.isFreight() && freight) {
                    head[temp] = (" ");
                    head[temp + 1] = ("货运");
                    head[temp + 2] = (" ");
                    headcell.add("完成单量");
                    headcell.add("订单金额");
                    headcell.add("提成");
                    freight = false;
                    continue;
                }
                if (serviceSetting.isZhuanxian() && zhuanxian) {
                    head[temp] = (" ");
                    head[temp + 1] = ("城际专线");
                    head[temp + 2] = (" ");
                    headcell.add("完成单量");
                    headcell.add("订单金额");
                    headcell.add("提成");
                    zhuanxian = false;
                    continue;
                }
            }
			/*if(serviceSetting.isDaijia()){
				head[3]=(" ");
				head[4]=("代驾");
				head[5]=(" ");
				headcell.add("完成单量");
				headcell.add("订单金额");
				headcell.add("提成");
			}
			if(serviceSetting.isPaotui()){
				head[6]=(" ");
				head[7]=("跑腿");
				head[8]=(" ");
				headcell.add("完成单量");
				headcell.add("订单金额");
				headcell.add("提成");
			}
			if(serviceSetting.isZhuanche()){
				head[9]=(" ");
				head[10]=("专车");
				head[11]=(" ");
				headcell.add("完成单量");
				headcell.add("订单金额");
				headcell.add("提成");
			}
			if(serviceSetting.isFreight()){
				head[12]=(" ");
				head[13]=("货运");
				head[14]=(" ");
				headcell.add("完成单量");
				headcell.add("订单金额");
				headcell.add("提成");
			}*/
            rowlist.add(headcell);
            for (DirverStatistics a : list.getContent()) {
                List<String> celllist = new ArrayList<String>();
                if (a.getName().contains("总计")) {
                    celllist.add(a.getName());
                    celllist.add(" ");
                    celllist.add(" ");
                    if (serviceSetting.isDaijia()) {
                        celllist.add(a.getDaijiaFinishNums() + "");
                        celllist.add(a.getDaijiaMoney() + "");
                        celllist.add(a.getDaijiaTicheng() + "");
                    }
                    if (serviceSetting.isPaotui()) {
                        celllist.add(a.getErrandFinishNums() + "");
                        celllist.add(a.getErrandMoney() + "");
                        celllist.add(a.getErrandTicheng() + "");
                    }
                    if (serviceSetting.isZhuanche()) {
                        celllist.add(a.getZhuancheFinishNums() + "");
                        celllist.add(a.getZhuancheMoney() + "");
                        celllist.add(a.getZhuancheTicheng() + "");
                    }
                    if (serviceSetting.isFreight()) {
                        celllist.add(a.getFreightFinishNums() + "");
                        celllist.add(a.getFreightMoney() + "");
                        celllist.add(a.getFreightTicheng() + "");
                    }
                    if (serviceSetting.isZhuanxian()) {
                        celllist.add(a.getZhuanxianFinishNums() + "");
                        celllist.add(a.getZhuanxianMoney() + "");
                        celllist.add(a.getZhuanxianTicheng() + "");
                    }
                } else {
                    celllist.add(a.getName() + "(" + a.getUsername() + ")");
                    celllist.add(a.getType());
                    celllist.add(a.getCompanyName());
                    if (serviceSetting.isDaijia()) {
                        celllist.add(a.getDaijiaFinishNums() + "");
                        celllist.add(a.getDaijiaMoney() + "");
                        celllist.add(a.getDaijiaTicheng() + "");
                    }
                    if (serviceSetting.isPaotui()) {
                        celllist.add(a.getErrandFinishNums() + "");
                        celllist.add(a.getErrandMoney() + "");
                        celllist.add(a.getErrandTicheng() + "");
                    }
                    if (serviceSetting.isZhuanche()) {
                        celllist.add(a.getZhuancheFinishNums() + "");
                        celllist.add(a.getZhuancheMoney() + "");
                        celllist.add(a.getZhuancheTicheng() + "");
                    }
                    if (serviceSetting.isFreight()) {
                        celllist.add(a.getFreightFinishNums() + "");
                        celllist.add(a.getFreightMoney() + "");
                        celllist.add(a.getFreightTicheng() + "");
                    }
                    if (serviceSetting.isZhuanxian()) {
                        celllist.add(a.getZhuanxianFinishNums() + "");
                        celllist.add(a.getZhuanxianMoney() + "");
                        celllist.add(a.getZhuanxianTicheng() + "");
                    }
                }
                rowlist.add(celllist);
            }

            ExcelUtil.export(req, response, head, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }

    //员工业绩初始化
    @RequestMapping("driverOrderDetail/{id}")
    public String infoFeeDetail(Model model,
        @PathVariable("id") Long driverId) {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            if (serviceSetting.isDaijia()) {
                model.addAttribute("activityTab", "daijia");
            } else if (serviceSetting.isZhuanche()) {
                model.addAttribute("activityTab", "zhuan");
            } else if (serviceSetting.isPaotui()) {
                model.addAttribute("activityTab", "errand");
            } else if (serviceSetting.isFreight()) {
                model.addAttribute("activityTab", "freight");
            } else if (serviceSetting.isZhuanxian()) {
                model.addAttribute("activityTab", "zhuanxian");
            }

            model.addAttribute("driver", driverService.findOne(driverId));
            return "chart/driverOrderDetail";

        } else {
            throw new NoPermissionException();
        }
    }

    //员工业绩详细
    @RequestMapping("driverOrderDetailCount")
    public @ResponseBody
    Map<String, PageVo<DriverOrderDetailVo>> driverOrderDetailCount(
        Long driverId,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<DriverOrderDetailVo> daijia = null;
            PageVo<DriverOrderDetailVo> errand = null;
            PageVo<DriverOrderDetailVo> zhuan = null;
            PageVo<DriverOrderDetailVo> freight = null;
            PageVo<DriverOrderDetailVo> zhuanxian = null;
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            if (serviceSetting.isDaijia()) {
                daijia = chartYearService.driverOrderDetailCount(driverId, start, end);
            }
            if (serviceSetting.isPaotui()) {
                errand = errandService.driverOrderDetailCount(driverId, start, end);
            }
            if (serviceSetting.isZhuanche()) {
                zhuan = zhuancheService.driverOrderDetailCount(driverId, start, end);
            }
            if (serviceSetting.isFreight()) {
                freight = freightService.driverOrderDetailCount(driverId, start, end);
            }

            if (serviceSetting.isZhuanxian()) {
                zhuanxian = zhuanxianService.driverOrderDetailCount(driverId, start, end);
            }

            Map<String, PageVo<DriverOrderDetailVo>> map = new HashMap<String, PageVo<DriverOrderDetailVo>>();
            map.put("daijia", daijia);
            map.put("zhuan", zhuan);
            map.put("errand", errand);
            map.put("freight", freight);
            map.put("zhuanxian", zhuanxian);
            return map;
        } else {
            throw new NoPermissionException();
        }

    }

    //员工业绩详细
    @RequestMapping("driverOrderDetailDate")
    public @ResponseBody
    Map<String, PageVo<DriverOrderDetailVo>> infoFeeDetail(
        Long driverId,
        @RequestParam("type") String type,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
            PageVo<DriverOrderDetailVo> daijia = null;
            PageVo<DriverOrderDetailVo> errand = null;
            PageVo<DriverOrderDetailVo> zhuan = null;
            PageVo<DriverOrderDetailVo> freight = null;
            PageVo<DriverOrderDetailVo> zhuanxian = null;
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            if (serviceSetting.isDaijia()) {
                if ("daijia".equals(type)) {
                    daijia = chartYearService.driverOrderDetail(driverId, start, end);
                }
            }
            if (serviceSetting.isPaotui()) {
                if ("errand".equals(type)) {
                    errand = errandService.driverOrderDetail(driverId, start, end);
                }
            }
            if (serviceSetting.isZhuanche()) {
                if ("zhuanche".equals(type)) {
                    zhuan = zhuancheService.driverOrderDetail(driverId, start, end);
                }
            }
            if (serviceSetting.isFreight()) {
                if ("freight".equals(type)) {
                    freight = freightService.driverOrderDetail(driverId, start, end);
                }
            }

            if (serviceSetting.isZhuanxian()) {
                if ("zhuanxian".equals(type)) {
                    zhuanxian = zhuanxianService.driverOrderDetail(driverId, start, end);
                }

            }

            Map<String, PageVo<DriverOrderDetailVo>> map = new HashMap<String, PageVo<DriverOrderDetailVo>>();
            map.put("daijia", daijia);
            map.put("zhuan", zhuan);
            map.put("errand", errand);
            map.put("freight", freight);
            map.put("zhuanxian", zhuanxian);
            return map;
        } else {
            throw new NoPermissionException();
        }

    }
    //导出员工业绩详细

    @RequestMapping("exportDetail")
    public void exportDetail(
        @RequestParam(required = false, value = "driverId") Long driverId,
        @RequestParam(required = false, value = "exportStartTime") String startTime,
        @RequestParam(required = false, value = "exportEndTime") String endTime,
        String[] heards, boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(start);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    start = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.DAY_OF_MONTH, -30);
                start = calendar.getTime();
            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(end);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);

                    end = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                end = calendar.getTime();
            }

            List<DriverOrderDetailVo> list = chartYearService
                .exportDriverOrderDetail(driverId, start, end, allAmount, amounts);

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            List<List<String>> rowlist = new ArrayList<List<String>>();
            for (DriverOrderDetailVo a : list) {

                List<String> celllist = new ArrayList<String>();
                if (null != heards) {
                    if (arrayContains(heards, "时间")) {
                        celllist.add(formatter.format(a.getFinishTime()));
                    }
                    if (arrayContains(heards, "订单号")) {
                        celllist.add(a.getOrderNum());
                    }
                    if (arrayContains(heards, "金额(元)")) {
                        celllist.add(a.getMoney().toString());
                    }
                    if (arrayContains(heards, "提成(元)")) {
                        celllist.add(a.getTicheng().toString());
                    }

                    rowlist.add(celllist);
                }

            }

            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }

    //公司统计初始化
    @RequestMapping("companyOrder")
    public ModelAndView companyOrder(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {
        ModelAndView mv = new ModelAndView();
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.DAY_OF_MONTH, -30);
                start = calendar.getTime();
            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                end = calendar.getTime();
            }

            List<CompanyOrderVo> result = chartYearService.companyOrder(start, end);
            mv.addObject("startTime", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(start));
            mv.addObject("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(end));
            mv.addObject("result", result);
            mv.addObject("totalElements", result.size() - 1);
            mv.setViewName("/chart/companyOrder");
            return mv;

        } else {
            throw new NoPermissionException();
        }

    }

    //公司统计
	/*@RequestMapping("companyOrderDate")
	public  @ResponseBody List<CompanyOrderVo> companyOrderDate(
			@RequestParam(required = false, value = "startTime") String startTime,
			@RequestParam(required = false, value = "endTime") String endTime) {

		Subject subject = SecurityUtils.getSubject();

		if(subject.isPermitted("statistics")){

			Date start = null;
			Date end = null;


			if (!StringUtils.isEmpty(startTime)) {

				try {
					start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(start);
					calendar.set(Calendar.HOUR_OF_DAY, 0);
					calendar.set(Calendar.MINUTE, 0);
					calendar.set(Calendar.SECOND, 0);

					start = calendar.getTime();
				} catch (ParseException e) {
					e.printStackTrace();
				}

			}else{
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.add(Calendar.DAY_OF_MONTH, -30);
				start = calendar.getTime();
			}

			if (!StringUtils.isBlank(endTime)) {

				try {
					end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

					Calendar calendar = Calendar.getInstance();
					calendar.setTime(end);
					calendar.set(Calendar.HOUR_OF_DAY, 23);
					calendar.set(Calendar.MINUTE, 59);
					calendar.set(Calendar.SECOND, 59);

					end = calendar.getTime();
				} catch (ParseException e) {
					e.printStackTrace();
				}

			}else{
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				end=calendar.getTime();
			}


			List<CompanyOrderVo> result=chartYearService.companyOrder(start,end);
			return result;

		}
		else{
			throw new NoPermissionException();
		}

	}*/

    //导出公司统计

    @RequestMapping("exportCompany")
    public void exportCompany(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            List<CompanyOrderVo> list = chartYearService.companyOrder(start, end);
            List<List<String>> rowlist = new ArrayList<List<String>>();
            List<String> heardcoll = new ArrayList<String>();
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            heardcoll.add("公司名称");
            String[] heards = new String[16];
            heards[0] = "#";
            int serviceCount = 0;
            if (serviceSetting.isDaijia()) {
                serviceCount++;
            }
            if (serviceSetting.isPaotui()) {
                serviceCount++;
            }
            if (serviceSetting.isZhuanche()) {
                serviceCount++;
            }
            if (serviceSetting.isFreight()) {
                serviceCount++;
            }
            if (serviceSetting.isZhuanxian()) {
                serviceCount++;
            }
            boolean daijia = true, pao = true, zhuan = true, freight = true, zhuanxian = true;
            for (int i = 0; i < serviceCount; i++) {
                int temp = i * 3 + 1;
                if (serviceSetting.isDaijia() && daijia) {
                    heards[temp] = " ";
                    heards[temp + 1] = "代驾";
                    heards[temp + 2] = " ";
                    heardcoll.add("完成单量");
                    heardcoll.add("销单量");
                    heardcoll.add("订单金额");
                    daijia = false;
                    continue;
                }

                if (serviceSetting.isZhuanche() && zhuan) {
                    heards[temp] = " ";
                    heards[temp + 1] = "专车";
                    heards[temp + 2] = " ";
                    heardcoll.add("完成单量");
                    heardcoll.add("销单量");
                    heardcoll.add("订单金额");
                    zhuan = false;
                    continue;
                }
                if (serviceSetting.isPaotui() && pao) {
                    heards[temp] = " ";
                    heards[temp + 1] = "跑腿";
                    heards[temp + 2] = " ";
                    heardcoll.add("完成单量");
                    heardcoll.add("销单量");
                    heardcoll.add("订单金额");
                    pao = false;
                    continue;
                }
                if (serviceSetting.isFreight() && freight) {
                    heards[temp] = " ";
                    heards[temp + 1] = "货运";
                    heards[temp + 2] = " ";
                    heardcoll.add("完成单量");
                    heardcoll.add("销单量");
                    heardcoll.add("订单金额");
                    freight = false;
                    continue;
                }
                if (serviceSetting.isZhuanxian() && zhuanxian) {
                    heards[temp] = " ";
                    heards[temp + 1] = "城际专线";
                    heards[temp + 2] = " ";
                    heardcoll.add("完成单量");
                    heardcoll.add("销单量");
                    heardcoll.add("订单金额");
                    zhuanxian = false;
                    continue;
                }
            }



			/*if(serviceSetting.isDaijia()){
				heards[1]=" ";
				heards[2]="代驾";
				heards[3]=" ";
				heardcoll.add("完成单量");
				heardcoll.add("销单量");
				heardcoll.add("订单金额");
			}

			if(serviceSetting.isPaotui()){
				heards[4]=" ";
				heards[5]="跑腿";
				heards[6]=" ";
				heardcoll.add("完成单量");
				heardcoll.add("销单量");
				heardcoll.add("订单金额");
			}
			if(serviceSetting.isZhuanche()){
				heards[7]=" ";
				heards[8]="专车";
				heards[9]=" ";
				heardcoll.add("完成单量");
				heardcoll.add("销单量");
				heardcoll.add("订单金额");
			}
			if(serviceSetting.isFreight()){
				heards[10]=" ";
				heards[11]="货运";
				heards[12]=" ";
				heardcoll.add("完成单量");
				heardcoll.add("销单量");
				heardcoll.add("订单金额");
			}*/
            rowlist.add(heardcoll);
            for (CompanyOrderVo a : list) {
                List<String> celllist = new ArrayList<String>();
                celllist.add(a.getCompanyName());
                if (serviceSetting.isDaijia()) {
                    celllist.add(a.getFinishNums() + "");
                    celllist.add(a.getCancelNums() + "");
                    celllist
                        .add(a.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "");
                }
                if (serviceSetting.isZhuanche()) {
                    celllist.add(a.getZhuanche().getFinishNums() + "");
                    celllist.add(a.getZhuanche().getCancelNums() + "");
                    celllist.add(
                        a.getZhuanche().getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toString()
                            + "");
                }
                if (serviceSetting.isPaotui()) {
                    celllist.add(a.getErrand().getFinishNums() + "");
                    celllist.add(a.getErrand().getCancelNums() + "");
                    celllist.add(
                        a.getErrand().getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toString()
                            + "");
                }
                if (serviceSetting.isFreight()) {
                    celllist.add(a.getFreight().getFinishNums() + "");
                    celllist.add(a.getFreight().getCancelNums() + "");
                    celllist.add(
                        a.getFreight().getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toString()
                            + "");
                }
                if (serviceSetting.isZhuanxian()) {
                    celllist.add(a.getZhuanxian().getFinishNums() + "");
                    celllist.add(a.getZhuanxian().getCancelNums() + "");
                    celllist.add(
                        a.getZhuanxian().getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toString()
                            + "");
                }
                rowlist.add(celllist);

            }

            ExcelUtil.export(req, response, heards, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }


    //保险费/信息费
    @RequestMapping("infoFee")
    public String infoFee(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            model.addAttribute("companys", companyService.findAll());
            model.addAttribute("totalElements",
                chartYearService.infoFee(null, null, null).getTotalElements());
            return "chart/infoFee";

        } else {
            throw new NoPermissionException();
        }

    }

    //保险费/信息费
    @RequestMapping("infoFeeData")
    public @ResponseBody
    PageVo<InfoFeeVo> infoFeeData(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<InfoFeeVo> result = chartYearService.infoFee(start, end, companyId);

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //导出数据
    @RequestMapping("exportInfor")
    public void exportInfor(
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportStartTime") String startTime,
        @RequestParam(required = false, value = "exportEndTime") String endTime,
        String[] heards, boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {
                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            if (!StringUtils.isBlank(endTime)) {
                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            List<InfoFeeVo> list = chartYearService
                .exportInfoFee(start, end, companyId, allAmount, amounts);

            List<List<String>> rowlist = new ArrayList<List<String>>();

            for (InfoFeeVo a : list) {

                List<String> celllist = new ArrayList<String>();
                if (null != heards) {
                    if (arrayContains(heards, "代驾员")) {
                        celllist.add(a.getName());
                    }
                    if (arrayContains(heards, "所属公司")) {
                        celllist.add(a.getCompanyName());
                    }

                    if (arrayContains(heards, "保险费/信息费")) {
                        celllist.add(a.getMoney().toString());
                    }

                    rowlist.add(celllist);
                }

            }
            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }


    //订单类型
    @RequestMapping("orderType")
    public String orderType(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            model.addAttribute("companys", companyService.findAll());

            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            if (serviceSetting.isDaijia()) {
                model.addAttribute("activityTab", "daijia");
            } else if (serviceSetting.isZhuanche()) {
                model.addAttribute("activityTab", "zhuan");
            } else if (serviceSetting.isPaotui()) {
                model.addAttribute("activityTab", "errand");
            } else if (serviceSetting.isFreight()) {
                model.addAttribute("activityTab", "freight");
            } else if (serviceSetting.isZhuanxian()) {
                model.addAttribute("activityTab", "zhuanxian");
            }

            return "chart/orderType";
        } else {
            throw new NoPermissionException();
        }

    }


    //初始化订单来源
    @RequestMapping("orderFromSource")
    public String orderFromSource(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            model.addAttribute("companys", companyService.findAll());
            model.addAttribute("totalElements",
                chartYearService.orderFromSource(null, null, null).getTotalElements());
            return "chart/orderFromSource";

        } else {
            throw new NoPermissionException();
        }

    }

    //订单来源
    @RequestMapping("orderFromSourceDate")
    public @ResponseBody
    PageVo<OrderFromSourceVo> orderFromSourceDate(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {

            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<OrderFromSourceVo> result = null;
            if (principal.isCompanyType()) {
                result = chartYearService.orderFromSource(start, end, companyId);
            } else {
                result = chartYearService.orderFromSource(start, end, principal.getCompanyId());
            }

            return result;

        } else {
            throw new NoPermissionException();
        }

    }


    //跑腿订单来源
    @RequestMapping(value = "errandOrderFromSourceDate", method = RequestMethod.POST)
    public @ResponseBody
    PageVo<OrderFromSourceVo> errandOrderFromSourceDate(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<OrderFromSourceVo> result = null;
            if (principal.isCompanyType()) {
                result = errandService.orderFromSource(start, end, companyId);
            } else {
                result = errandService.orderFromSource(start, end, principal.getCompanyId());
            }

            return result;

        } else {
            throw new NoPermissionException();
        }

    }

    //货运订单来源
    @RequestMapping(value = "freightOrderFromSourceDate", method = RequestMethod.POST)
    public @ResponseBody
    PageVo<OrderFromSourceVo> freightOrderFromSourceDate(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<OrderFromSourceVo> result = null;
            if (principal.isCompanyType()) {
                result = freightService.orderFromSource(start, end, companyId);
            } else {
                result = freightService.orderFromSource(start, end, principal.getCompanyId());
            }

            return result;

        } else {
            throw new NoPermissionException();
        }

    }

    //专车订单来源
    @RequestMapping(value = "zhuancheOrderFromSourceDate", method = RequestMethod.POST)
    public @ResponseBody
    PageVo<OrderFromSourceVo> zhuancheOrderFromSourceDate(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<OrderFromSourceVo> result = null;
            if (principal.isCompanyType()) {
                result = zhuancheService.orderFromSource(start, end, companyId);
            } else {
                result = zhuancheService.orderFromSource(start, end, principal.getCompanyId());
            }

            return result;

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "zhuanxianOrderFromSourceDate", method = RequestMethod.POST)
    public @ResponseBody
    PageVo<OrderFromSourceVo> zhuanxianOrderFromSourceDate(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<OrderFromSourceVo> result = null;
            if (principal.isCompanyType()) {
                result = zhuanxianService.orderFromSource(start, end, companyId);

            } else {
                result = zhuanxianService.orderFromSource(start, end, principal.getCompanyId());
            }

            return result;

        } else {
            throw new NoPermissionException();
        }

    }

    //导出订单来源数据
    @RequestMapping(value = "exportOrderFromSource", method = RequestMethod.GET)
    public void exportOrderFromSource(
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportStartTime") String startTime,
        @RequestParam(required = false, value = "exportEndTime") String endTime,
        String[] heards, boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            Principal principal = (Principal) subject.getPrincipal();
            List<OrderFromSourceVo> list = null;
            if (principal.isCompanyType()) {
                list = chartYearService.exportSource(start, end, companyId, amounts);
            } else {
                list = chartYearService.exportSource(start, end, principal.getCompanyId(), amounts);
            }

            List<List<String>> rowlist = new ArrayList<List<String>>();

            for (OrderFromSourceVo a : list) {

                List<String> celllist = new ArrayList<String>();
                if (null != heards) {
                    if (arrayContains(heards, "订单来源")) {
                        celllist.add(a.getFromSource());
                    }
                    if (arrayContains(heards, "订单数量")) {
                        celllist.add(a.getAmount() + "");
                    }

                    if (arrayContains(heards, "金额")) {
                        celllist.add(a.getMoney().toString());
                    }

                    rowlist.add(celllist);
                }

            }

            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "memberOrder1", method = RequestMethod.GET)
    public ModelAndView memberOrder1() {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            ModelAndView mv = new ModelAndView();

            mv.addObject("grades", memberService.listAllGrades());
            if (principal.isCompanyType()) {
                mv.addObject("companys", companyService.findAll());
            }

            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            if (serviceSetting.isDaijia()) {
                mv.addObject("activityTab", "daijia");
            } else if (serviceSetting.isZhuanche()) {
                mv.addObject("activityTab", "zhuan");
            } else if (serviceSetting.isPaotui()) {
                mv.addObject("activityTab", "errand");
            } else if (serviceSetting.isFreight()) {
                mv.addObject("activityTab", "freight");
            } else if (serviceSetting.isZhuanxian()) {
                mv.addObject("activityTab", "zhuanxian");
            }

            mv.setViewName("chart/memberOrder1");

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    //会员消费
    @RequestMapping("memberOrderData")
    public @ResponseBody
    PageVo<MemberOrderVo> memberOrderData(
        @RequestParam(required = false, value = "type") Integer type,
        @RequestParam(required = false, value = "gradeId") Long gradeId,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        @RequestParam(required = false, value = "memberName") String name,
        @RequestParam(required = false, value = "companyId") Long companyId
    ) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<MemberOrderVo> result = null;
            if (principal.isCompanyType()) {
                result = chartYearService.memberOrder(start, end, name, companyId, gradeId, type);
            } else {
                result = chartYearService
                    .memberOrder(start, end, name, principal.getCompanyId(), gradeId, type);
            }
            return result;
        } else {
            throw new NoPermissionException();
        }

    }


    //导出会员消费记录
    @RequestMapping("exportMemberOrder")
    public void exportMemberOrder(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        @RequestParam(required = false, value = "memberName") String name,
        @RequestParam(required = false, value = "type") Integer type,
        @RequestParam(required = false, value = "gradeId") Long gradeId,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<MemberOrderVo> list = null;
            if (principal.isCompanyType()) {
                list = chartYearService.memberOrder(start, end, name, companyId, gradeId, type);
            } else {
                list = chartYearService
                    .memberOrder(start, end, name, principal.getCompanyId(), gradeId, type);
            }

            String[] head = new String[4];
            List<List<String>> rowlist = new ArrayList<List<String>>();

            head[0] = ("客户");
            head[1] = ("所属公司");
            head[2] = ("订单完成数量（单）");
            head[3] = ("订单完成金额（元）");
            for (MemberOrderVo a : list.getContent()) {
                List<String> celllist = new ArrayList<String>();

                celllist.add(a.getName() + "-" + a.getPhone());
                celllist.add(a.getCompanyName());
                celllist.add(a.getFinishNums() + "");
                celllist.add(a.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toString());

                rowlist.add(celllist);

            }

            ExcelUtil.export(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

    //导出
    @RequestMapping(value = "exportOrder", method = RequestMethod.GET)
    public void exportOrder(
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportStartTime") String startTime,
        @RequestParam(required = false, value = "exportEndTime") String endTime,
        @RequestParam(required = false, value = "exportName") String name,
        String[] heards, HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(start);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    start = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.DAY_OF_MONTH, -30);
                start = calendar.getTime();
            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(end);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);

                    end = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                end = calendar.getTime();
            }
            List<MemberOrderVo> list = null;
            if (principal.isCompanyType()) {
                list = chartYearService.newMemberOrder(start, end, companyId);
            } else {
                list = chartYearService.newMemberOrder(start, end, principal.getCompanyId());
            }

            List<List<String>> rowlist = new ArrayList<List<String>>();
            for (MemberOrderVo a : list) {

                List<String> celllist = new ArrayList<String>();
                if (null != heards) {
                    if (arrayContains(heards, "客户姓名")) {
                        celllist.add(a.getName());
                    }
                    if (arrayContains(heards, "客户电话")) {
                        celllist.add(a.getPhone());
                    }
                    if (arrayContains(heards, "所属公司")) {
                        celllist.add(a.getCompanyName());

                    }
                    if (arrayContains(heards, "订单数(单)")) {
                        celllist.add(a.getFinishNums() + "");
                    }
                    if (arrayContains(heards, "金额(元)")) {
                        celllist.add(a.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }

                    rowlist.add(celllist);
                }

            }

            ExcelUtil.export(req, response, heards, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }


    //订单统计表格初始化 月
    @RequestMapping("tableOrderMonth")
    public String tableOrderMonth(Model model) {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();
            int nowYear = calendar.get(Calendar.YEAR);
            List<Integer> years = new LinkedList<Integer>();
            for (int i = nowYear; i >= 2014; i--) {
                years.add(i);
            }

            model.addAttribute("years", years);
            model.addAttribute("yearInt", calendar.get(Calendar.YEAR));
            model.addAttribute("companys", companyService.findAll());
            //model.addAttribute("result", chartYearService.tableOrderMonth(calendar,null));
            return "chart/tableOrderMonth";
        } else {
            throw new NoPermissionException();
        }
    }


    //订单统计表格 月
    @RequestMapping("tableOrderMonthData")
    public @ResponseBody
    List<TableOrderMonthVo> tableOrderMonthData(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            List<TableOrderMonthVo> result = null;
            if (principal.isCompanyType()) {
                result = chartYearService.tableOrderMonth(calendar, companyId);
            } else {
                result = chartYearService.tableOrderMonth(calendar, principal.getCompanyId());
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //跑腿订单统计表格 月
    @RequestMapping("errandTableOrderMonthData")
    public @ResponseBody
    List<TableOrderMonthVo> errandTableOrderMonthData(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            List<TableOrderMonthVo> result = null;
            if (principal.isCompanyType()) {
                result = errandService.tableOrderMonth(year, companyId);
            } else {
                result = errandService.tableOrderMonth(year, principal.getCompanyId());
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //专车订单统计表格 月  zhuancheTableOrderMonthData
    @RequestMapping("zhuancheTableOrderMonthData")
    public @ResponseBody
    List<TableOrderMonthVo> zhuancheTableOrderMonthData(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            List<TableOrderMonthVo> result = null;
            if (principal.isCompanyType()) {
                result = zhuancheService.tableOrderMonth(year, companyId);
            } else {
                result = zhuancheService.tableOrderMonth(year, principal.getCompanyId());
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //专车订单统计表格 月  zhuancheTableOrderMonthData
    @RequestMapping("zhuanxianTableOrderMonthData")
    public @ResponseBody
    List<TableOrderMonthVo> zhuanxianTableOrderMonthData(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            List<TableOrderMonthVo> result = null;
            if (principal.isCompanyType()) {
                result = zhuanxianService.tableOrderMonth(year, companyId);
            } else {
                result = zhuanxianService.tableOrderMonth(year, principal.getCompanyId());
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //货运订单统计表格 月 freightTableOrderMonthData
    @RequestMapping("freightTableOrderMonthData")
    public @ResponseBody
    List<TableOrderMonthVo> freightTableOrderMonthData(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            List<TableOrderMonthVo> result = null;
            if (principal.isCompanyType()) {
                result = freightService.tableOrderMonth(year, companyId);
            } else {
                result = freightService.tableOrderMonth(year, principal.getCompanyId());
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //导出月统计订单数据
    @RequestMapping(value = "export", method = RequestMethod.GET)
    public void export(
        @RequestParam(required = false, value = "exportYear") Integer year,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportMonth") Integer month,
        @RequestParam(required = false, value = "exportDay") Integer day,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            if (year != null && month == null && day == null) {
                String[] heards = new String[13];
                heards[0] = "类型\\月份";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = i + "月份";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<TableOrderMonthVo> list = null;
                if (principal.isCompanyType()) {
                    list = chartYearService.exportTableOrderMonth(calendar, companyId);
                } else {
                    list = chartYearService
                        .exportTableOrderMonth(calendar, principal.getCompanyId());
                }

                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (TableOrderMonthVo a : list) {
                        if (i == a.getTime()) {
                            celllist1.add(a.getCancelAmount() + "");
                            celllist2.add(a.getFinishedAmount() + "");
                            celllist3.add(a.getMoney().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);
            } else if (year != null && month != null && day == null) {

                ChartYear dataForYear = chartYearService.dataForYear(year, companyId);
                ChartMonth months = new ChartMonth();
                for (ChartMonth mo : dataForYear.getMonths()) {
                    if (month.intValue() == mo.getMonth()) {
                        months = mo;
                        break;
                    }
                }
                List<ChartDay> list = months.getChartDays();
                String[] heards = new String[months.getDays() + 1];
                heards[0] = "类型\\天";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = i + "天";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (ChartDay a : list) {
                        if (i == a.getDay()) {
                            celllist1.add(a.getTotalCancelNum() + "");
                            celllist2.add(a.getTotalSuccessNum() + "");
                            celllist3.add(a.getTotalPrice().toString());
                            break;
                        }
                    }

                }
                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);

            } else if (year != null && month != null && day != null) {
                ChartDay chartDay = chartYearService.dataForDay(year, month, day, companyId);
                List<HourChart> list = chartDay.getHours();
                String[] heards = new String[25];
                heards[0] = "类型\\小时";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i - 1) + "时";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 0; i < 24; i++) {

                    for (HourChart a : list) {
                        if (i == a.getHour()) {
                            celllist1.add(a.getTotalCancel() + "");
                            celllist2.add(a.getTotalSuccess() + "");
                            celllist3.add(a.getTotalPrice().toString());
                            break;
                        }
                    }

                }
                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);
            }

        } else {
            throw new NoPermissionException();
        }

    }

    //
    @RequestMapping(value = "errandExport", method = RequestMethod.GET)
    public void errandExport(
        @RequestParam("exportYear") Integer year,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportMonth") Integer month,
        @RequestParam(required = false, value = "exportDay") Integer day,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }

            if (year != null && month == null && day == null) {
                String[] heards = new String[13];
                heards[0] = "类型\\月份";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = i + "月份";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<TableOrderMonthVo> list = null;
                if (principal.isCompanyType()) {
                    list = errandService.tableOrderMonth(year, companyId);
                } else {
                    list = errandService.tableOrderMonth(year, principal.getCompanyId());
                }

                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (TableOrderMonthVo a : list) {
                        if (i == (a.getTime() + 1)) {
                            celllist1.add(a.getCancelAmount() + "");
                            celllist2.add(a.getFinishedAmount() + "");
                            celllist3.add(a.getMoney().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);


            } else if (year != null && month != null && day == null) {
                ChartMonth chartMonth = null;
                if (principal.isCompanyType()) {
                    chartMonth = errandService.errandDataForMonth(year, month, companyId);
                } else {
                    chartMonth = errandService
                        .errandDataForMonth(year, month, principal.getCompanyId());
                }

                List<ChartDay> list = chartMonth.getChartDays();
                String[] heards = new String[chartMonth.getDays() + 1];
                heards[0] = "类型\\天";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i) + "天";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (ChartDay a : list) {
                        if (i == (a.getDay())) {
                            celllist1.add(a.getTotalCancelNum() + "");
                            celllist2.add(a.getTotalSuccessNum() + "");
                            celllist3.add(a.getTotalPrice().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);

            } else if (year != null && month != null && day != null) {
                ChartDay chartDay = null;
                if (principal.isCompanyType()) {
                    chartDay = errandService.errandDataForDay(year, month, day, companyId);
                } else {
                    chartDay = errandService
                        .errandDataForDay(year, month, day, principal.getCompanyId());
                }

                List<HourChart> list = chartDay.getHours();
                String[] heards = new String[25];
                heards[0] = "类型\\小时";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i - 1) + "时";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 0; i < heards.length; i++) {

                    for (HourChart a : list) {
                        if (i == (a.getHour())) {
                            celllist1.add(a.getTotalCancel() + "");
                            celllist2.add(a.getTotalSuccess() + "");
                            celllist3.add(a.getTotalPrice().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);
            }


        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "zhuanxianExport", method = RequestMethod.GET)
    public void zhuanxianExport(
        @RequestParam("exportYear") Integer year,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportMonth") Integer month,
        @RequestParam(required = false, value = "exportDay") Integer day,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            if (year != null && month == null && day == null) {
                String[] heards = new String[13];
                heards[0] = "类型\\月份";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = i + "月份";
                }
                List<TableOrderMonthVo> list = null;
                if (principal.isCompanyType()) {
                    list = zhuanxianService.tableOrderMonth(year, companyId);
                } else {
                    list = zhuanxianService.tableOrderMonth(year, principal.getCompanyId());
                }

                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (TableOrderMonthVo a : list) {
                        if (i == (a.getTime() + 1)) {
                            celllist1.add(a.getCancelAmount() + "");
                            celllist2.add(a.getFinishedAmount() + "");
                            celllist3.add(a.getMoney().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);


            } else if (year != null && month != null && day == null) {
                ChartMonth chartMonth = null;
                if (principal.isCompanyType()) {
                    chartMonth = zhuanxianService.zhuanxianDataForMonth(year, month, companyId);
                } else {
                    chartMonth = zhuanxianService
                        .zhuanxianDataForMonth(year, month, principal.getCompanyId());
                }

                List<ChartDay> list = chartMonth.getChartDays();
                String[] heards = new String[chartMonth.getDays() + 1];
                heards[0] = "类型\\天";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i) + "天";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (ChartDay a : list) {
                        if (i == (a.getDay())) {
                            celllist1.add(a.getTotalCancelNum() + "");
                            celllist2.add(a.getTotalSuccessNum() + "");
                            celllist3.add(a.getTotalPrice().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);

            } else if (year != null && month != null && day != null) {
                ChartDay chartDay = null;
                if (principal.isCompanyType()) {
                    chartDay = zhuanxianService.zhuanxianDataForDay(year, month, day, companyId);
                } else {
                    chartDay = zhuanxianService
                        .zhuanxianDataForDay(year, month, day, principal.getCompanyId());
                }

                List<HourChart> list = chartDay.getHours();
                String[] heards = new String[25];
                heards[0] = "类型\\小时";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i - 1) + "时";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 0; i < heards.length; i++) {

                    for (HourChart a : list) {
                        if (i == (a.getHour())) {
                            celllist1.add(a.getTotalCancel() + "");
                            celllist2.add(a.getTotalSuccess() + "");
                            celllist3.add(a.getTotalPrice().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);
            }


        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "freightExport", method = RequestMethod.GET)
    public void freightExport(
        @RequestParam("exportYear") Integer year,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportMonth") Integer month,
        @RequestParam(required = false, value = "exportDay") Integer day,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            if (year != null && month == null && day == null) {
                String[] heards = new String[13];
                heards[0] = "类型\\月份";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = i + "月份";
                }
                List<TableOrderMonthVo> list = null;
                if (principal.isCompanyType()) {
                    list = freightService.tableOrderMonth(year, companyId);
                } else {
                    list = freightService.tableOrderMonth(year, principal.getCompanyId());
                }

                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (TableOrderMonthVo a : list) {
                        if (i == (a.getTime() + 1)) {
                            celllist1.add(a.getCancelAmount() + "");
                            celllist2.add(a.getFinishedAmount() + "");
                            celllist3.add(a.getMoney().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);


            } else if (year != null && month != null && day == null) {
                ChartMonth chartMonth = null;
                if (principal.isCompanyType()) {
                    chartMonth = freightService.freightDataForMonth(year, month, companyId);
                } else {
                    chartMonth = freightService
                        .freightDataForMonth(year, month, principal.getCompanyId());
                }

                List<ChartDay> list = chartMonth.getChartDays();
                String[] heards = new String[chartMonth.getDays() + 1];
                heards[0] = "类型\\天";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i) + "天";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {

                    for (ChartDay a : list) {
                        if (i == (a.getDay())) {
                            celllist1.add(a.getTotalCancelNum() + "");
                            celllist2.add(a.getTotalSuccessNum() + "");
                            celllist3.add(a.getTotalPrice().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);

            } else if (year != null && month != null && day != null) {
                ChartDay chartDay = null;
                if (principal.isCompanyType()) {
                    chartDay = freightService.freightDataForDay(year, month, day, companyId);
                } else {
                    chartDay = freightService
                        .freightDataForDay(year, month, day, principal.getCompanyId());
                }

                List<HourChart> list = chartDay.getHours();
                String[] heards = new String[25];
                heards[0] = "类型\\小时";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i - 1) + "时";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 0; i < heards.length; i++) {

                    for (HourChart a : list) {
                        if (i == (a.getHour())) {
                            celllist1.add(a.getTotalCancel() + "");
                            celllist2.add(a.getTotalSuccess() + "");
                            celllist3.add(a.getTotalPrice().toString());
                        }
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);
            }


        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "zhuancheExport", method = RequestMethod.GET)
    public void zhuancheExport(
        @RequestParam("exportYear") Integer year,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportMonth") Integer month,
        @RequestParam(required = false, value = "exportDay") Integer day,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
		/*	String[] heards=new String[13];
			heards[0]="类型\\月份";
			for(int i=1;i<heards.length;i++){
				heards[i]=i+"月份";
			}
			//String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
			List<TableOrderMonthVo> list=zhuancheService.tableOrderMonth(year, companyId);
			List<List<String>> rowlist=new ArrayList<List<String>>();
			List<String> celllist1=new ArrayList<String>();
			celllist1.add("销单量");
			List<String> celllist2=new ArrayList<String>();
			celllist2.add("完成单量");
			List<String> celllist3=new ArrayList<String>();
			celllist3.add("金额(元)");
			for(int i=1;i<heards.length;i++){

				for(TableOrderMonthVo a:list){
					if(i==(a.getTime()+1)){
						celllist1.add(a.getCancelAmount()+"");
						celllist2.add(a.getFinishedAmount()+"");
						celllist3.add(a.getMoney().toString());
					}
				}

			}


			rowlist.add(celllist1);
			rowlist.add(celllist2);
			rowlist.add(celllist3);


			ExcelUtil.export(req,response,heards,rowlist);
	  */
            if (year != null && month == null && day == null) {
                String[] heards = new String[13];
                heards[0] = "类型\\月份";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = i + "月份";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<TableOrderMonthVo> list = null;
                if (principal.isCompanyType()) {
                    list = zhuancheService.tableOrderMonth(year, companyId);
                } else {
                    list = zhuancheService.tableOrderMonth(year, principal.getCompanyId());
                }

                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {
                    String cancel = "";
                    String success = "";
                    String price = "";
                    boolean flag = false;
                    for (TableOrderMonthVo a : list) {
                        if (i == (a.getTime() + 1)) {
                            cancel = a.getCancelAmount() + "";
                            success = a.getFinishedAmount() + "";
                            price = a.getMoney().toString();
                            flag = true;
                        }
                    }
                    if (flag) {
                        celllist1.add(cancel);
                        celllist2.add(success);
                        celllist3.add(price);
                    } else {
                        celllist1.add("0");
                        celllist2.add("0");
                        celllist3.add("0");
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);


            } else if (year != null && month != null && day == null) {
                ChartMonth chartMonth = null;
                if (principal.isCompanyType()) {
                    chartMonth = zhuancheService.zhuancheDataForMonth(year, month, companyId);
                } else {
                    chartMonth = zhuancheService
                        .zhuancheDataForMonth(year, month, principal.getCompanyId());
                }

                List<ChartDay> list = chartMonth.getChartDays();
                String[] heards = new String[chartMonth.getDays() + 1];
                heards[0] = "类型\\天";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i) + "天";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 1; i < heards.length; i++) {
                    String cancel = "";
                    String success = "";
                    String price = "";
                    boolean flag = false;
                    for (ChartDay a : list) {
                        if (i == (a.getDay())) {
                            cancel = a.getTotalCancelNum() + "";
                            success = a.getTotalSuccessNum() + "";
                            price = a.getTotalPrice().toString();
                            flag = true;
                        }
                    }
                    if (flag) {
                        celllist1.add(cancel);
                        celllist2.add(success);
                        celllist3.add(price);
                    } else {
                        celllist1.add("0");
                        celllist2.add("0");
                        celllist3.add("0");
                    }


                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);

            } else if (year != null && month != null && day != null) {
                ChartDay chartDay = null;
                if (principal.isCompanyType()) {
                    chartDay = zhuancheService.zhuancheDataForDay(year, month, day, companyId);
                } else {
                    chartDay = zhuancheService
                        .zhuancheDataForDay(year, month, day, principal.getCompanyId());
                }

                List<HourChart> list = chartDay.getHours();
                String[] heards = new String[25];
                heards[0] = "类型\\小时";
                for (int i = 1; i < heards.length; i++) {
                    heards[i] = (i - 1) + "时";
                }
                //String[] heards,boolean tableHeader,boolean allAmount,Integer amounts,String type,
                List<List<String>> rowlist = new ArrayList<List<String>>();
                List<String> celllist1 = new ArrayList<String>();
                celllist1.add("销单量");
                List<String> celllist2 = new ArrayList<String>();
                celllist2.add("完成单量");
                List<String> celllist3 = new ArrayList<String>();
                celllist3.add("金额(元)");
                for (int i = 0; i < heards.length - 1; i++) {
                    String cancel = "";
                    String success = "";
                    String price = "";
                    boolean flag = false;
                    for (HourChart a : list) {

                        if (i == (a.getHour())) {
                            cancel = a.getTotalCancel() + "";
                            success = a.getTotalSuccess() + "";
                            price = a.getTotalPrice().toString();
                            flag = true;
                        }
                    }
                    if (flag) {
                        celllist1.add(cancel);
                        celllist2.add(success);
                        celllist3.add(price);
                    } else {
                        celllist1.add("0");
                        celllist2.add("0");
                        celllist3.add("0");
                    }

                }

                rowlist.add(celllist1);
                rowlist.add(celllist2);
                rowlist.add(celllist3);

                ExcelUtil.export(req, response, heards, rowlist);
            }


        } else {
            throw new NoPermissionException();
        }

    }

    //订单统计表格初始化
    @RequestMapping("tableOrderDay")
    public String tableOrderDay(Model model,
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "month") Integer month,
        @RequestParam(required = false, value = "companyId") Long companyId) {
        List<Integer> years = new LinkedList<Integer>();
        List<Integer> months = new LinkedList<Integer>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        if (null != year) {
            calendar.set(Calendar.YEAR, year);
        }
        if (null != month) {
            calendar.set(Calendar.MONTH, month - 1);
        }

        int nowYear = calendar.get(Calendar.YEAR);

        for (int i = nowYear; i >= 2014; i--) {
            years.add(i);
        }
        for (int i = 1; i <= 12; i++) {
            months.add(i);
        }
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (principal.isCompanyType()) {
            model.addAttribute("result", chartYearService.tableOrderDay(calendar, companyId));
        } else {
            model.addAttribute("result",
                chartYearService.tableOrderDay(calendar, principal.getCompanyId()));
        }
        model.addAttribute("year", year);
        model.addAttribute("month", month);
        model.addAttribute("months", months);
        model.addAttribute("years", years);
        model.addAttribute("companyId", companyId);

        return "chart/tableOrderDay";
    }


    //订单统计表格 日
    @RequestMapping("tableOrderDayDate")
    public @ResponseBody
    List<TableOrderMonthVo> tableOrderDay(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "month") Integer month,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            if (null != month) {
                calendar.set(Calendar.MONTH, month - 1);
            }
            List<TableOrderMonthVo> result = null;
            if (principal.isCompanyType()) {
                result = chartYearService.tableOrderDay(calendar, companyId);
            } else {
                result = chartYearService.tableOrderDay(calendar, principal.getCompanyId());
            }
            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //导出数据
    @RequestMapping("exportDay")
    public void exportDay(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "month") Integer month,
        String[] heards, boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }
            if (null != month) {
                calendar.set(Calendar.MONTH, month - 1);
            }

            int nowYear = calendar.get(Calendar.YEAR);

            List<Integer> years = new LinkedList<Integer>();
            List<Integer> months = new LinkedList<Integer>();

            for (int i = nowYear; i >= 2014; i--) {
                years.add(i);
            }
            for (int i = 1; i <= 12; i++) {
                months.add(i);
            }

            List<TableOrderMonthVo> list = chartYearService
                .exportTableOrderDay(calendar, allAmount, amounts);

            List<List<String>> rowlist = new ArrayList<List<String>>();

            for (TableOrderMonthVo t : list) {

                List<String> celllist = new ArrayList<String>();

                if (null != heards) {
                    if (arrayContains(heards, "年份")) {
                        celllist.add(year + "/" + month + "/" + t.getTime());
                    }

                    if (arrayContains(heards, "月份")) {
                        celllist.add(t.getTime() + "");
                    }
                    if (arrayContains(heards, "销单量")) {
                        celllist.add(t.getCancelAmount() + "");
                    }
                    if (arrayContains(heards, "完成单量")) {
                        celllist.add(t.getFinishedAmount() + "");

                    }

                    if (arrayContains(heards, "金额")) {
                        celllist.add(t.getMoney().toString());
                    }

                    rowlist.add(celllist);
                }

            }

            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }


    //活动统计
    @RequestMapping("activity")
    public String activity(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            model.addAttribute("companys", companyService.findAll());
            model.addAttribute("totalElements",
                chartYearService.activity(null, null, null, null).getTotalElements());
            return "chart/activity";

        } else {
            throw new NoPermissionException();
        }

    }

    //活动统计数据
    @RequestMapping("activityData")
    public @ResponseBody
    PageVo<Activity> activityData(
        @RequestParam(required = false, value = "title") String title,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<Activity> result = chartYearService.activity(companyId, title, start, end);

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    //导出活动数据
    @RequestMapping("exportActivity")
    public void exportActivity(
        @RequestParam(required = false, value = "exportTitle") String title,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportStartTime") String startTime,
        @RequestParam(required = false, value = "exportEndTime") String endTime,
        String[] heards, boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            List<Activity> list = chartYearService
                .exportActivty(companyId, title, start, end, allAmount, amounts);

            List<List<String>> rowlist = new ArrayList<List<String>>();

            for (Activity a : list) {

                List<String> celllist = new ArrayList<String>();
                if (null != heards) {
                    if (arrayContains(heards, "公司")) {
                        celllist.add(a.getCompany().getCompanyName());
                    }
                    if (arrayContains(heards, "类型")) {
                        if (a.getActivityType().equals(ActivityType.cash)) {
                            celllist.add("返现");
                        } else {
                            celllist.add("返卷");
                        }
                    }
                    if (arrayContains(heards, "参与人次")) {
//						celllist.add(a.getJoinAmount().toString());
                    }
                    if (arrayContains(heards, "参与金额")) {
//						celllist.add(a.getTotalMoney().toString());
                    }

                    rowlist.add(celllist);
                }

            }

            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }

    //司机在线时间初始化
    @RequestMapping("driverTime")
    public String driverTime(Model model) {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();

            model.addAttribute("companys", companyService.findAll());
            model.addAttribute("totalElements",
                driverTimeService.countDriverTime(calendar.get(Calendar.YEAR), null, null));
            model.addAttribute("Month", calendar.get(Calendar.MONTH) + 1);
            model.addAttribute("yearsstart", calendar.get(Calendar.YEAR));

            return "chart/driverTime";

        } else {
            throw new NoPermissionException();
        }

    }

    //司机在线时间统计
    @RequestMapping("driverTimeData")
    public @ResponseBody
    Map<String, Object> driverTimeData(
        @RequestParam(required = false, value = "name") String name,
        @RequestParam(required = true, value = "startyear") Integer startyear,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startmonth") Integer startmonth) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, startyear);
        calendar.set(Calendar.MONTH, startmonth - 1);

        Map<String, Object> map = new HashMap<String, Object>();

        Page<DriverTimeVo> driverTimeVos = driverTimeService
            .pageDriverTime(calendar, name, companyId);

        List<Object> listDay = new ArrayList<Object>();
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 1; i <= day; i++) {
            listDay.add(i);
        }

        map.put("listDay", listDay);

        map.put("dataList", driverTimeVos);

        return map;

    }

    @RequestMapping("countDriverTime")
    public @ResponseBody
    Long countDriverTime(
        @RequestParam(required = false, value = "name") String name,
        @RequestParam(required = true, value = "startyear") Integer startyear,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startmonth") Integer startmonth
    ) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, startyear);
        calendar.set(Calendar.MONTH, startmonth - 1);

        return driverTimeService.countDriverTime(calendar.get(Calendar.YEAR), name, companyId);

    }

    //导出司机在线时间数据
    @RequestMapping(value = "exportDriverTime", method = RequestMethod.GET)
    public void exportTime(
        @RequestParam(required = false, value = "exportName") String name,
        @RequestParam(required = false, value = "exportYear") Integer startyear,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportMonth") Integer startmonth,
        boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

//		Calendar calendar=Calendar.getInstance();
//		//获取当前月
//		int month = calendar.get(Calendar.MONTH)+1;
//		calendar.set(Calendar.YEAR, startyear);
//		int day = 0;
//		if(month > startmonth){
//			calendar.set(Calendar.MONTH, startmonth-1);
//			day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//			calendar.set(Calendar.DAY_OF_MONTH, day);
//		}else{
//			day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//		}

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, startyear);
        calendar.set(Calendar.MONTH, startmonth - 1);

        List<DriverTimeVo> driverTimeVos = driverTimeService
            .exportDriverTime(calendar, name, companyId, allAmount, amounts);
        int day = 0;
        day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String[] heards = null;
        if (tableHeader) {
            heards = new String[day + 3];
            heards[0] = "姓名(工号)";
            heards[1] = "时间";
            for (int i = 0; i < day; i++) {
                heards[i + 2] = String.valueOf(i + 1);
            }
            heards[heards.length - 1] = "月计";
        }

        List<List<String>> rowlist = new ArrayList<List<String>>();

        if (CollectionUtils.isNotEmpty(driverTimeVos)) {
            for (DriverTimeVo d : driverTimeVos) {

                List<String> celllist = new ArrayList<String>();

                celllist.add(d.getRealname() + "(" + d.getUsername() + ")");

                celllist.add(startyear + "/" + startmonth);

                for (int i = 0; i < d.getDayWorkTime().size(); i++) {
                    celllist.add(d.getDayWorkTime().get(i).toString());
                }

                celllist.add(d.getWorkTime().toString());

                rowlist.add(celllist);

            }
        }

        ExcelUtil.export(req, response, heards, rowlist);

    }


    //推荐人员工业绩统计
    @RequestMapping("introduce")
    public ModelAndView introduce(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "param") String param,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {
        ModelAndView mv = new ModelAndView();
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            mv.addObject("companyId", companyId);
            mv.addObject("startTime", startTime);
            mv.addObject("endTime", endTime);
            mv.addObject("param", param != null ? param : "");
            mv.addObject("companys", companyService.findAll());
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            PageVo<DriverOrderVo> page = null;
            if (principal.isCompanyType()) {
                page = chartYearService.introduce(companyId, param, start, end);
            } else {
                page = chartYearService.introduce(principal.getCompanyId(), param, start, end);
            }
            mv.addObject("totalElements", page.getContent().size());
            mv.addObject("page", page);
            mv.setViewName("chart/introduce");
            return mv;

        } else {
            throw new NoPermissionException();
        }

    }

    //推荐人员工业绩统计总条数
    @RequestMapping("introduceTotal")
    public @ResponseBody
    long introduceTotal(Long companyId, String param) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            if (principal.isCompanyType()) {
                return chartYearService.countIntroduce(companyId, param);
            } else {
                return chartYearService.countIntroduce(principal.getCompanyId(), param);
            }
        } else {
            throw new NoPermissionException();
        }

    }


    //推荐人员工业绩统计数据
    @RequestMapping("introduceData")
    public @ResponseBody
    PageVo<DriverOrderVo> introduceData(Model model,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "param") String param,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(start);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    start = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.DAY_OF_MONTH, -30);
                start = calendar.getTime();
            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(end);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);

                    end = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                end = calendar.getTime();
            }
            PageVo<DriverOrderVo> result = null;
            if (principal.isCompanyType()) {
                result = chartYearService.introduce(companyId, param, start, end);
            } else {
                result = chartYearService.introduce(principal.getCompanyId(), param, start, end);
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("exportIntroduce")
    public void exportIntroduce(
        @RequestParam(required = false, value = "exportParam") String param,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        @RequestParam(required = false, value = "exportStartTime") String startTime,
        @RequestParam(required = false, value = "exportEndTime") String endTime,
        String[] heards, boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(start);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    start = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(end);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);

                    end = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
            List<DriverOrderVo> list = null;
            if (principal.isCompanyType()) {
                list = chartYearService
                    .exportIntroduce(companyId, param, start, end, allAmount, amounts);
            } else {
                list = chartYearService
                    .exportIntroduce(principal.getCompanyId(), param, start, end, allAmount,
                        amounts);
            }
            List<List<String>> rowlist = new ArrayList<List<String>>();

            for (DriverOrderVo d : list) {

                List<String> celllist = new ArrayList<String>();
                if (null != heards) {
                    if (arrayContains(heards, "推荐人")) {
                        celllist.add(d.getIntroduced());
                    }
                    if (arrayContains(heards, "代驾员(工号)")) {
                        celllist.add(d.getName() + "(" + d.getUsername() + ")");
                    }
                    if (arrayContains(heards, "员工类型")) {
                        celllist.add(d.getType());
                    }
                    if (arrayContains(heards, "所属公司")) {
                        celllist.add(d.getCompanyName());
                    }
                    if (arrayContains(heards, "订单数（单）")) {
                        celllist.add(String.valueOf(d.getFinishNums()));
                    }
                    if (arrayContains(heards, "金额（元）")) {
                        celllist.add(String.valueOf(d.getMoney()));
                    }
                    if (arrayContains(heards, "提成（元）")) {
                        celllist.add(String.valueOf(d.getTicheng()));
                    }
                    if (arrayContains(heards, "余额（元）")) {
                        celllist.add(String.valueOf(d.getVirtual()));
                    }

                    rowlist.add(celllist);
                }

            }

            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("compositive")
    public String compositive(Model model, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {

            if (principal.isCompanyType()) {
                if (null != companyId) {
                    model.addAttribute("companyId", companyId);
                } else {
                    model.addAttribute("companyId", principal.getCompanyId());
                }
                model.addAttribute("companys", companyService.findAll());
                return "chart/compositive";
            } else {
                model.addAttribute("companyId", principal.getCompanyId());
                return "chart/compositive";
            }

        } else {
            throw new NoPermissionException();
        }

    }

    //活动统计数据
    @RequestMapping("compositiveData")
    public @ResponseBody
    Map<String, Object> compositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (principal.isCompanyType()) {

                Map<String, Object> result = chartYearService.compositive(companyId, start, end);
                result.put("companyName", companyService.findOne(companyId).getCompanyName());

                return result;

            } else {
                Map<String, Object> result = chartYearService
                    .compositive(principal.getCompanyId(), start, end);
                result.put("companyName",
                    companyService.findOne(principal.getCompanyId()).getCompanyName());

                return result;
            }


        } else {
            throw new NoPermissionException();
        }

    }

    //导出
    @RequestMapping(value = "exportOrderAll", method = RequestMethod.GET)
    public void exportOrderAll(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "companyIdAll") Boolean companyIdAll,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            List<OrderFinishedInfo> list = null;
            if (companyIdAll != null && companyIdAll) {
                list = orderFinishedInfoService.findBycompanyId(null, start, end);
            } else if (principal.isCompanyType()) {
                list = orderFinishedInfoService.findBycompanyId(companyId, start, end);
            } else {
                list = orderFinishedInfoService
                    .findBycompanyId(principal.getCompanyId(), start, end);
            }
            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[14];
            head[0] = ("订单号");
            head[1] = ("完成时间");
            head[2] = ("实收金额");
            head[3] = ("订单公司");
            head[4] = ("优惠券金额");
            head[5] = ("代付金额");
            head[6] = ("签单金额");
            head[7] = ("余额金额");
            head[8] = ("支付宝金额");
            head[9] = ("微信金额");
            head[10] = ("过路费");
            head[11] = ("服务人员收入");
            head[12] = ("公司收入");
            head[13] = ("客户经理收入");

            for (OrderFinishedInfo ofi : list) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                List<String> celllist = new ArrayList<String>();
                celllist.add(ofi.getOrderNumber());
                celllist.add(sdf.format(ofi.getFinishTime()));
                celllist.add(String.valueOf(ofi.getRealCash()));
                if (principal.isCompanyType()) {
                    Company company = companyService.findOne(ofi.getCompanyId());
                    celllist.add(company.getAbbreviation());
                } else {
                    celllist
                        .add(companyService.findOne(principal.getCompanyId()).getAbbreviation());
                }
                celllist.add(String.valueOf(ofi.getCouponMoney()));
                celllist.add(String.valueOf(ofi.getCashMoney()));
                celllist.add(String.valueOf(ofi.getSignMoney()));
                celllist.add(String.valueOf(ofi.getBalanceMoney()));
                celllist.add(String.valueOf(ofi.getAlipayMoney()));
                celllist.add(String.valueOf(ofi.getWeixinMoney()));
                celllist.add(String.valueOf(ofi.getGuoluMoney()));
                celllist.add(String.valueOf(ofi.getDriverMoney()));
                celllist.add(String.valueOf(ofi.getCompanyMoney()));
                celllist.add(String.valueOf(ofi.getWxManagerMoney()));
                rowlist.add(celllist);

            }

            ExcelUtil.export(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping("paotuiCompositive")
    public String paotuiCompositive(Model model, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {

            if (principal.isCompanyType()) {
                if (null != companyId) {
                    model.addAttribute("companyId", companyId);
                } else {
                    model.addAttribute("companyId", principal.getCompanyId());
                }
                model.addAttribute("companys", companyService.findAll());
                return "chart/paotuiCompositive";
            } else {
                model.addAttribute("companyId", principal.getCompanyId());
                return "chart/paotuiCompositive";
            }

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("zhuanCompositive")
    public String zhuanCompositive(Model model, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {

            if (principal.isCompanyType()) {
                if (null != companyId) {
                    model.addAttribute("companyId", companyId);
                } else {
                    model.addAttribute("companyId", principal.getCompanyId());
                }
                model.addAttribute("companys", companyService.findAll());
                return "chart/zhuanCompositive";
            } else {
                model.addAttribute("companyId", principal.getCompanyId());
                return "chart/zhuanCompositive";
            }

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("freightCompositive")
    public String freightCompositive(Model model, Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {

            if (principal.isCompanyType()) {
                if (null != companyId) {
                    model.addAttribute("companyId", companyId);
                } else {
                    model.addAttribute("companyId", principal.getCompanyId());
                }
                model.addAttribute("companys", companyService.findAll());
                return "chart/freightCompositive";
            } else {
                model.addAttribute("companyId", principal.getCompanyId());
                return "chart/freightCompositive";
            }

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("zhuanxianCompositive")
    public ModelAndView zhuanxianCompositive(Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        ModelAndView mv = new ModelAndView();
        if (subject.isPermitted("statistics")) {

            if (principal.isCompanyType()) {
                if (null != companyId) {
                    mv.addObject("companyId", companyId);
                } else {
                    mv.addObject("companyId", principal.getCompanyId());
                }
                mv.addObject("lines", zhuanxianService.queryLines(null));
                mv.addObject("companys", companyService.findAll());

            } else {
                mv.addObject("lines", zhuanxianService.queryLines(principal.getCompanyId()));
                mv.addObject("companyId", principal.getCompanyId());
            }

            mv.setViewName("chart/zhuanxianCompositive");
            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    //活动统计数据
    @RequestMapping("zhuanxianCompositiveData")
    public @ResponseBody
    Map<String, Object> zhuanCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "lineId") Long lineId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (principal.isCompanyType()) {

                Map<String, Object> result = chartYearService
                    .zhuanxianCompositive(companyId, lineId, start, end);
                result.put("companyName", companyService.findOne(companyId).getCompanyName());

                return result;

            } else {
                Map<String, Object> result = chartYearService
                    .zhuanxianCompositive(principal.getCompanyId(), lineId, start, end);
                result.put("companyName",
                    companyService.findOne(principal.getCompanyId()).getCompanyName());

                return result;
            }


        } else {
            throw new NoPermissionException();
        }

    }

    //活动统计数据
    @RequestMapping("zhuanCompositiveData")
    public @ResponseBody
    Map<String, Object> zhuanCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (principal.isCompanyType()) {

                Map<String, Object> result = chartYearService
                    .zhuanCompositive(companyId, start, end);
                result.put("companyName", companyService.findOne(companyId).getCompanyName());

                return result;

            } else {
                Map<String, Object> result = chartYearService
                    .zhuanCompositive(principal.getCompanyId(), start, end);
                result.put("companyName",
                    companyService.findOne(principal.getCompanyId()).getCompanyName());

                return result;
            }


        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("exportZhuanCompositiveData")
    public @ResponseBody
    void exportZhuanCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            List<com.ybkj.daijia.server.zhuan.Order> list = null;
            if (principal.isCompanyType()) {
                list = zhuancheService.findOrderFinishedBycompanyId(companyId, start, end);
            } else {
                list = zhuancheService
                    .findOrderFinishedBycompanyId(principal.getCompanyId(), start, end);
            }
            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[13];
            head[0] = ("订单号");
            head[1] = ("完成时间");
            head[2] = ("实收金额");
            head[3] = ("订单公司");
            head[4] = ("优惠券金额");
            head[5] = ("代付金额");
            head[6] = ("签单金额");
            head[7] = ("余额金额");
            head[8] = ("支付宝金额");
            head[9] = ("微信金额");
            head[10] = ("过路费");
            head[11] = ("服务人员收入");
            head[12] = ("公司收入");

            for (com.ybkj.daijia.server.zhuan.Order ofi : list) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                List<String> celllist = new ArrayList<String>();
                celllist.add(ofi.getOrderNo());
                celllist.add(sdf.format(ofi.getFinishTime()));
                celllist.add(String.valueOf(ofi.getRealPay() == null ? 0 : ofi.getRealPay()));
                celllist.add(ofi.getCompanyName());
                celllist
                    .add(String.valueOf(ofi.getCouponMoney() == null ? 0 : ofi.getCouponMoney()));
                celllist.add(String.valueOf(ofi.getCashMoney() == null ? 0 : ofi.getCashMoney()));
                celllist.add(String.valueOf(ofi.getSignMoney() == null ? 0 : ofi.getSignMoney()));
                celllist
                    .add(String.valueOf(ofi.getBalanceMoney() == null ? 0 : ofi.getBalanceMoney()));
                celllist
                    .add(String.valueOf(ofi.getAlipayMoney() == null ? 0 : ofi.getAlipayMoney()));
                celllist
                    .add(String.valueOf(ofi.getWeixinMoney() == null ? 0 : ofi.getWeixinMoney()));
                celllist.add(String.valueOf(ofi.getGuoluMoney() == null ? 0 : ofi.getGuoluMoney()));
                celllist
                    .add(String.valueOf(ofi.getEmployProfit() == null ? 0 : ofi.getEmployProfit()));
                celllist.add(
                    String.valueOf(ofi.getCompanyProfit() == null ? 0 : ofi.getCompanyProfit()));
                rowlist.add(celllist);

            }

            ExcelUtil.export(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("freightCompositiveData")
    public @ResponseBody
    Map<String, Object> freightCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (principal.isCompanyType()) {

                Map<String, Object> result = chartYearService
                    .freightCompositive(companyId, start, end);
                result.put("companyName", companyService.findOne(companyId).getCompanyName());

                return result;

            } else {
                Map<String, Object> result = chartYearService
                    .freightCompositive(principal.getCompanyId(), start, end);
                result.put("companyName",
                    companyService.findOne(principal.getCompanyId()).getCompanyName());

                return result;
            }


        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("exportFreightCompositiveData")
    public @ResponseBody
    void exportFreightCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            List<com.ybkj.daijia.server.freight.Order> list = null;
            if (principal.isCompanyType()) {
                list = freightService.findOrderFinishedBycompanyId(companyId, start, end);
            } else {
                list = freightService
                    .findOrderFinishedBycompanyId(principal.getCompanyId(), start, end);
            }
            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[13];
            head[0] = ("订单号");
            head[1] = ("完成时间");
            head[2] = ("实收金额");
            head[3] = ("订单公司");
            head[4] = ("优惠券金额");
            head[5] = ("代付金额");
            head[6] = ("签单金额");
            head[7] = ("余额金额");
            head[8] = ("支付宝金额");
            head[9] = ("微信金额");
            head[10] = ("过路费");
            head[11] = ("服务人员收入");
            head[12] = ("公司收入");
            for (com.ybkj.daijia.server.freight.Order ofi : list) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                List<String> celllist = new ArrayList<String>();
                celllist.add(ofi.getOrderNo());
                celllist.add(sdf.format(ofi.getFinishTime()));
                celllist.add(String.valueOf(ofi.getRealPay()));
                celllist.add(ofi.getCompanyName());
                celllist
                    .add(String.valueOf(ofi.getCouponMoney() == null ? 0 : ofi.getCouponMoney()));
                celllist.add(String.valueOf(ofi.getCashMoney() == null ? 0 : ofi.getCashMoney()));
                celllist.add(String.valueOf(ofi.getSignMoney() == null ? 0 : ofi.getSignMoney()));
                celllist
                    .add(String.valueOf(ofi.getBalanceMoney() == null ? 0 : ofi.getBalanceMoney()));
                celllist
                    .add(String.valueOf(ofi.getAlipayMoney() == null ? 0 : ofi.getAlipayMoney()));
                celllist
                    .add(String.valueOf(ofi.getWeixinMoney() == null ? 0 : ofi.getWeixinMoney()));
                celllist.add(String.valueOf(ofi.getGuoluMoney() == null ? 0 : ofi.getGuoluMoney()));
                celllist
                    .add(String.valueOf(ofi.getEmployProfit() == null ? 0 : ofi.getEmployProfit()));
                celllist.add(
                    String.valueOf(ofi.getCompanyProfit() == null ? 0 : ofi.getCompanyProfit()));
                rowlist.add(celllist);

            }

            ExcelUtil.export(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("exportzhuanxianCompositiveData")
    public @ResponseBody
    void exportzhuanxianCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime,
        @RequestParam(required = false, value = "lineId") Long lineId,
        HttpServletRequest req, HttpServletResponse response) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            List<com.ybkj.daijia.server.zhuanxian.Order> list = null;
            if (principal.isCompanyType()) {
                list = zhuanxianService.findOrderFinishedBycompanyId(companyId, start, end, lineId);
            } else {
                list = zhuanxianService
                    .findOrderFinishedBycompanyId(principal.getCompanyId(), start, end, lineId);
            }
            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[13];
            head[0] = ("订单号");
            head[1] = ("完成时间");
            head[2] = ("实收金额");
            head[3] = ("订单公司");
            head[4] = ("优惠券金额");
            head[5] = ("代付金额");
            head[6] = ("签单金额");
            head[7] = ("余额金额");
            head[8] = ("支付宝金额");
            head[9] = ("微信金额");
            head[10] = ("过路费");
            head[11] = ("服务人员收入");
            head[12] = ("公司收入");
            for (com.ybkj.daijia.server.zhuanxian.Order ofi : list) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                List<String> celllist = new ArrayList<String>();
                celllist.add(ofi.getOrderNo());
                celllist.add(sdf.format(ofi.getFinishTime()));
                celllist.add(String.valueOf(ofi.getRealPay()));
                celllist.add(ofi.getCompanyName());
                celllist
                    .add(String.valueOf(ofi.getCouponMoney() == null ? 0 : ofi.getCouponMoney()));
                celllist.add(String.valueOf(ofi.getCashMoney() == null ? 0 : ofi.getCashMoney()));
                celllist.add(String.valueOf(ofi.getSignMoney() == null ? 0 : ofi.getSignMoney()));
                celllist
                    .add(String.valueOf(ofi.getBalanceMoney() == null ? 0 : ofi.getBalanceMoney()));
                celllist
                    .add(String.valueOf(ofi.getAlipayMoney() == null ? 0 : ofi.getAlipayMoney()));
                celllist
                    .add(String.valueOf(ofi.getWeixinMoney() == null ? 0 : ofi.getWeixinMoney()));
                celllist.add(String.valueOf(ofi.getGuoluMoney() == null ? 0 : ofi.getGuoluMoney()));
                celllist
                    .add(String.valueOf(ofi.getEmployProfit() == null ? 0 : ofi.getEmployProfit()));
                celllist.add(
                    String.valueOf(ofi.getCompanyProfit() == null ? 0 : ofi.getCompanyProfit()));
                rowlist.add(celllist);

            }

            ExcelUtil.export(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

    //活动统计数据
    @RequestMapping("paotuiCompositiveData")
    public @ResponseBody
    Map<String, Object> paotuiCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (principal.isCompanyType()) {

                Map<String, Object> result = chartYearService
                    .paotuiCompositive(companyId, start, end);
                result.put("companyName", companyService.findOne(companyId).getCompanyName());

                return result;

            } else {
                Map<String, Object> result = chartYearService
                    .paotuiCompositive(principal.getCompanyId(), start, end);
                result.put("companyName",
                    companyService.findOne(principal.getCompanyId()).getCompanyName());

                return result;
            }


        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("exportPaotuiCompositiveData")
    public @ResponseBody
    void exportPaotuiCompositiveData(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "startTime") String startTime,
        @RequestParam(required = true, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response) throws IOException {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;
            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            List<com.ybkj.daijia.server.errand.Order> list = null;
            if (principal.isCompanyType()) {
                list = errandService.findOrderFinishedBycompanyId(companyId, start, end);
            } else {
                list = errandService
                    .findOrderFinishedBycompanyId(principal.getCompanyId(), start, end);
            }
            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[13];
            head[0] = ("订单号");
            head[1] = ("完成时间");
            head[2] = ("实收金额");
            head[3] = ("订单公司");
            head[4] = ("优惠券金额");
            head[5] = ("代付金额");
            head[6] = ("签单金额");
            head[7] = ("余额金额");
            head[8] = ("支付宝金额");
            head[9] = ("微信金额");
            head[10] = ("过路费");
            head[11] = ("服务人员收入");
            head[12] = ("公司收入");
            for (com.ybkj.daijia.server.errand.Order ofi : list) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                List<String> celllist = new ArrayList<String>();
                celllist.add(ofi.getOrderNo());
                celllist.add(ofi.getFinishTime() == null ? "" : sdf.format(ofi.getFinishTime()));
                celllist.add(String.valueOf(ofi.getRealPay() == null ? "" : ofi.getRealPay()));
                celllist.add(ofi.getCompanyName());
                celllist
                    .add(String.valueOf(ofi.getCouponMoney() == null ? 0 : ofi.getCouponMoney()));
                celllist.add(String.valueOf(ofi.getCashMoney() == null ? 0 : ofi.getCashMoney()));
                celllist.add(String.valueOf(ofi.getSignMoney() == null ? 0 : ofi.getSignMoney()));
                celllist
                    .add(String.valueOf(ofi.getBalanceMoney() == null ? 0 : ofi.getBalanceMoney()));
                celllist
                    .add(String.valueOf(ofi.getAlipayMoney() == null ? 0 : ofi.getAlipayMoney()));
                celllist
                    .add(String.valueOf(ofi.getWeixinMoney() == null ? 0 : ofi.getWeixinMoney()));
                celllist.add(String.valueOf(ofi.getGuoluMoney() == null ? 0 : ofi.getGuoluMoney()));
                celllist
                    .add(String.valueOf(ofi.getDriverProfit() == null ? 0 : ofi.getDriverProfit()));
                celllist.add(
                    String.valueOf(ofi.getCompanyProfit() == null ? 0 : ofi.getCompanyProfit()));
                rowlist.add(celllist);
            }

            ExcelUtil.export(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("tableNewMemberMonth")
    public String tableNewMemberMonth(Model model) {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();
            int nowYear = calendar.get(Calendar.YEAR);
            List<Integer> years = new LinkedList<Integer>();
            for (int i = nowYear; i >= 2014; i--) {
                years.add(i);
            }

            model.addAttribute("years", years);
            model.addAttribute("yearInt", calendar.get(Calendar.YEAR));
            model.addAttribute("companys", companyService.findAll());
            return "chart/tableNewMemberMonth";
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping("tableNewMemberMonthData")
    public @ResponseBody
    List<List<String>> tableNewMemberMonthData(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }

            List<List<String>> result = chartYearService.tableNewMemberMonth(calendar, companyId);
            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "exportTableNewMemberMonth", method = RequestMethod.GET)
    public void exportTableNewMemberMonth(
        @RequestParam(required = false, value = "exportYear") Integer year,
        @RequestParam(required = false, value = "exportCompanyId") Long companyId,
        boolean tableHeader, String type,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();

            if (null != year) {
                calendar.set(Calendar.YEAR, year);
            }

            String[] heards = {"", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月",
                "11月", "12月"};

            List<List<String>> rowlist = chartYearService.tableNewMemberMonth(calendar, companyId);

            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping("tableNewMemberMonthDetail")
    public String tableNewMemberMonthDetail(Model model, String fromSource, Integer year,
        Integer month, Long companyId) {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            model.addAttribute("fromSource", fromSource);
            model.addAttribute("year", year);
            model.addAttribute("month", month);
            model.addAttribute("companyId", companyId);

            model.addAttribute("totalElements", chartYearService
                .tableNewMemberMonthDetailCount(fromSource, year, month, companyId));

            return "chart/tableNewMemberMonthDetail";
        } else {
            throw new NoPermissionException();
        }
    }

    //员工业绩详细
    @RequestMapping("tableNewMemberMonthDetailData")
    public @ResponseBody
    PageVo<Passenger> tableNewMemberMonthDetailData(
        @RequestParam(required = true, value = "fromSource") String fromSource,
        @RequestParam(required = true, value = "year") Integer year,
        @RequestParam(required = true, value = "month") Integer month,
        @RequestParam(required = false, value = "companyId") Long companyId) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            PageVo<Passenger> result = chartYearService
                .tableNewMemberMonthDetailData(fromSource, year, month, companyId);

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("exportTableNewMemberMonthDetail")
    public void exportTableNewMemberMonthDetail(
        @RequestParam(required = true, value = "fromSource") String fromSource,
        @RequestParam(required = true, value = "year") Integer year,
        @RequestParam(required = true, value = "month") Integer month,
        @RequestParam(required = false, value = "companyId") Long companyId,
        String[] heards, boolean tableHeader, boolean allAmount, Integer amounts, String type,
        HttpServletRequest req, HttpServletResponse response) throws IOException {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            List<Passenger> list = chartYearService
                .exportTableNewMemberMonthDetail(fromSource, year, month, companyId, allAmount,
                    amounts);

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

            List<List<String>> rowlist = new ArrayList<List<String>>();
            for (Passenger passenger : list) {

                List<String> celllist = new ArrayList<String>();
                if (null != heards) {
                    if (arrayContains(heards, "联系人")) {
                        celllist.add(passenger.getName());
                    }
                    if (arrayContains(heards, "客户来源")) {
                        celllist.add(passenger.getChannel_name());
                    }
                    if (arrayContains(heards, "手机号")) {
                        celllist.add(passenger.getPhone());
                    }
                    if (arrayContains(heards, "所属公司")) {
                        celllist.add(passenger.getCompanyName());
                    }
                    if (arrayContains(heards, "注册时间")) {
                        celllist.add(passenger.getCreated());
                    }
                    rowlist.add(celllist);
                }

            }

            ExcelUtil.export(req, response, tableHeader == true ? heards : null, rowlist);

        } else {
            throw new NoPermissionException();
        }

    }


    private boolean arrayContains(String[] heards, String str) {
        if (null == heards && StringUtils.isBlank(str)) {
            return false;
        }

        for (int i = 0; i < heards.length; i++) {
            if (heards[i].equals(str)) {
                return true;
            }
        }

        return false;
    }


    //分公司统计
    @RequestMapping("filialeOrder")
    public String filialeOrder(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {

            model.addAttribute("totalElements", chartYearService.companyOrder(null, null).size());
            return "chart/filialeOrder";

        } else {
            throw new NoPermissionException();
        }

    }


    //订单客户类型
    @RequestMapping("userType")
    public String userType(Model model,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("statistics")) {
            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {

                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(start);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    start = calendar.getTime();

                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.DAY_OF_MONTH, -30);
                start = calendar.getTime();

            }

            if (!StringUtils.isBlank(endTime)) {

                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(end);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);

                    end = calendar.getTime();

                } catch (ParseException e) {
                    e.printStackTrace();
                }

            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                end = calendar.getTime();

            }

            model.addAttribute("start", start);
            model.addAttribute("end", end);
            model.addAttribute("companys", companyService.findAll());
            model.addAttribute("companyId", companyId);
            model.addAttribute("page", chartYearService.userType(start, end, companyId));
            return "chart/userType";
        } else {
            throw new NoPermissionException();
        }

    }

    /*	@RequestMapping(value = "heatMap", method = RequestMethod.GET)
		public ModelAndView heatMap(Integer year) {

			Subject subject = SecurityUtils.getSubject();
			Principal principal = (Principal) subject.getPrincipal();

			if (subject.isPermitted("statistics")) {
				ModelAndView mv = new ModelAndView();
				Calendar calendar = Calendar.getInstance();
				List<Integer> years = new LinkedList<Integer>();

				for (int i = calendar.get(Calendar.YEAR); i >= 2014; i--) {
					years.add(i);
				}
				mv.addObject("years",years);
				if(year==null){
					mv.addObject("searchYear",calendar.get(Calendar.YEAR));
				}else{
					mv.addObject("searchYear",year);
				}
				Company company = companyService.findOne(principal.getCompanyId());

				mv.addObject("lng", company.getLongitude());
				mv.addObject("lat", company.getLatitude());
				mv.setViewName("chart/heatMap");
				return mv;
			}else {
				throw new NoPermissionException();
			}

		}
*/
    @RequestMapping("heatMap")
    public String heatMap(Model model) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("statistics")) {

            Calendar calendar = Calendar.getInstance();
            List<Integer> years = new LinkedList<Integer>();
            for (int i = calendar.get(Calendar.YEAR); i >= 2014; i--) {
                years.add(i);
            }

            Company company = companyService.findOne(principal.getCompanyId());
            model.addAttribute("years", years);
            model.addAttribute("lng", company.getLongitude());
            model.addAttribute("lat", company.getLatitude());

            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            if (serviceSetting.isDaijia()) {
                model.addAttribute("activityTab", "daijia");
            } else if (serviceSetting.isZhuanche()) {
                model.addAttribute("activityTab", "zhuan");
            } else if (serviceSetting.isPaotui()) {
                model.addAttribute("activityTab", "errand");
            } else if (serviceSetting.isFreight()) {
                model.addAttribute("activityTab", "freight");
            }

            return "chart/heatMap";

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "heatMapData", method = RequestMethod.POST)
    public @ResponseBody
    List<PointVo> heatMapData(
        @RequestParam(required = false, value = "year") Integer year,
        @RequestParam(required = false, value = "month") Integer month
    ) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("statistics")) {
            Calendar calendar = Calendar.getInstance();

            if (null == year) {
                year = calendar.get(Calendar.YEAR);
            }
            if (null == month) {
                month = calendar.get(Calendar.MONTH);
            }
            Date start = null;
            Date end = null;
            try {
                start = DateUtils
                    .parseDate(year + "-" + (month > 9 ? month : ("0" + month)) + "-01",
                        "yyyy-MM-dd");//2015-01-01

                if (month != 12) {
                    end = DateUtils.parseDate(
                        year + "-" + ((month + 1) > 9 ? (month + 1) : "0" + (month + 1)) + "-01",
                        "yyyy-MM-dd");
                } else {
                    end = DateUtils
                        .parseDate((year + 1) + "-" + "01" + "-01", "yyyy-MM-dd");//2015-01-01
                }
            } catch (ParseException e) {

                e.printStackTrace();
            }
            //查询本月数据
            List<Order> list = orderService.findMonthOrder(start, end);
            List<Order> matched = new LinkedList<Order>();

            List<PointVo> processed = new LinkedList<PointVo>();
            for (int i = 0; i < list.size(); i++) {
                Order order = list.get(i);

                if (matched.contains(order)) {
                    continue;
                }

                PointVo pointVo = new PointVo();
                if (order.getClientLatitude() != null && order.getClientLongitude() != null) {
                    pointVo.lat = order.getClientLatitude();
                    pointVo.lng = order.getClientLongitude();
                }
                pointVo.count = 1;
                processed.add(pointVo);

                for (int j = i + 1; j < list.size(); j++) {

                    Order order2 = list.get(j);

                    if (matched.contains(order2)) {
                        continue;
                    }

                    PointVo pointVo2 = new PointVo();
                    if (order2.getClientLatitude() != null && order2.getClientLongitude() != null) {
                        pointVo2.lat = order2.getClientLatitude();
                        pointVo2.lng = order2.getClientLongitude();
                    }
                    if (pointVo.isSameLocation(pointVo, pointVo2)) { //相同位置
                        pointVo.count++;
                        matched.add(order2);//加入已匹配列表
                    }

                }

            }

            return processed;
        } else {
            throw new NoPermissionException();
        }

    }


}
